package cn.gov.msa.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.msa.model.MenuPermission;
import cn.gov.msa.service.MenuPermissionService;
import cn.gov.msa.util.InfoObjUtil;
import cn.gov.msa.util.RedisUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value="/menuPermission")
public class MenuPermissionController {
	
	@Autowired
	private MenuPermissionService menuPermissionService;
	
	@RequestMapping(value="/save")
	public String save(@RequestBody JSONObject param)throws Exception{
		 boolean flag =false;
		 int result = 0;
		 String info = "";
		 try {
			 if(param.has("roleCode")) {
				 List<MenuPermission> menuPermissions = menuPermissionService.queryByRoleCode(param.getString("roleCode"));
				 if(null!=menuPermissions&&menuPermissions.size()>0)
					 menuPermissionService.deleteByRoleCode(param.getString("roleCode"));
				 JSONArray menuLists = param.getJSONArray("menuLists");
				 List<MenuPermission> menuPermissionParams = new ArrayList<MenuPermission>();
				 ObjectMapper om = new ObjectMapper();
				 for (int i = 0; i < menuLists.size(); i++) {
					MenuPermission menuPermission  = om.readValue(menuLists.getJSONObject(i).toString(), MenuPermission.class);
					menuPermission.setRoleCode(param.getString("roleCode"));
					menuPermissionParams.add(menuPermission);
				}
				flag =  menuPermissionService.saveData(menuPermissionParams);
			 }
		 }catch(Exception e) {
			 result = 2;
			 info =  InfoObjUtil.Exption_msg;
		 }
		  JSONObject jsonResult = new JSONObject();
		    if(flag){
		    	result = 1;
		    	info = InfoObjUtil.Success_save_OK;
		    	
		    	try {
			    	RedisUtils redisUtils = new RedisUtils();
			    	redisUtils.delete("queryLeftMenu*");
				} catch (Exception e) {
					System.err.println("清除缓存失败！MenuPermissionController  56行");
				}
		    }
		    else{
		    	result = 0;
		    	info = InfoObjUtil.Error_save_no;
		    }
		    

	    	jsonResult.put("result", result);
	    	jsonResult.put("info", info);
		    
			return jsonResult.toString();
	}
	
	@RequestMapping(value="/queryByRoleCode")
	public String queryByRoleCode(String roleCode)throws Exception{
		 int result =0;
		 String info = "";
		List<MenuPermission> menuPermissions = menuPermissionService.queryByRoleCode(roleCode);
		JSONObject jsonResult = new JSONObject();
		if(null!=menuPermissions ) {
			JSONArray mps = JSONArray.fromObject(menuPermissions);
			jsonResult.put("datas", mps);
			result = 1;
			info =  InfoObjUtil.Success_select_OK;
		}else {
			result = 0;
			info = InfoObjUtil.Error_NODATA;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		
		return jsonResult.toString();
		
	}
}
